<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>for循环</title>
</head>
<body>
    <h2>for循环</h2>
    <p>for循环用于固定次数的循环</p>

</body>
<script>
    for (let i = 0;i < 10;i ++) {
        console.log("i =",i);
    }


    // 求1到100的累加值
    let sum = 0;
    for (let i = 1;i <= 100;i ++) {
        sum += i;
    }
    console.log("sum = ",sum);

    // 定义一个正整数n,判断n是不是质数？只能被1和自身整除的数，叫做质数
    let n = 17;
    // 是否是质数
    let flag = true;
    for (let i = 2;i < n;i ++) {
        // 如果能整除，那么久不是质数
        if (n % i == 0) {
            flag = false;
            break;
        } 
    }
    if (flag) {
        console.log(n,"是质数");
    } else {
        console.log(n,"不是质数");
    }

    n = 35;
    // 是否是质数
    flag = true;
    for (let i = 2;i < n;i ++) {
        // 如果能整除，那么久不是质数
        if (n % i == 0) {
            flag = false;
            break;
        } 
    }
    if (flag) {
        console.log(n,"是质数");
    } else {
        console.log(n,"不是质数");
    }
</script>
</html>